#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <sys/stat.h>

using namespace std;

// 遍历目录
void travDir(const string &path)
{
    // 打开文件夹
    DIR *dir = opendir(path.c_str());

    struct dirent *pdirent = NULL;
    while ((pdirent = readdir(dir)) != NULL)
    {
        // 忽略当前目录和上一级
        if (strcmp(pdirent->d_name, ".") == 0 || strcmp(pdirent->d_name, "..") == 0)
            continue;

        // 构建完整的文件/目录路径
        string fullPath = path + "/" + pdirent->d_name;

        // 判断文件类型
        if (pdirent->d_type == DT_DIR) 
        {
            cout << "目录:     " << fullPath << endl;
            // 递归遍历子目录
            travDir(fullPath);
        } 
        else if (pdirent->d_type == DT_REG) 
        {
            cout << "普通文件: " << fullPath << endl;
        } 
        else 
        {
            cout << "其他类型文件: " << fullPath << endl;
        }
        // cout << "d_type:" << (int)(pdirent->d_type) << "  d_name:" << pdirent->d_name << endl;
    }

    // 关闭文件夹
    closedir(dir);
}

int main()
{
    string startPath = "/home/hmq/code/267-class/系统调用/FileIO"; // 绝对路径
    travDir(startPath);

    return 0;  
}